package com.michaelbulava.TFS.ui.controls.workspaces;

import com.microsoft.tfs.core.clients.versioncontrol.path.LocalPath;
import com.microsoft.tfs.core.clients.versioncontrol.path.ServerPath;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.RecursionType;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolder;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolderType;
import com.microsoft.tfs.util.Check;

public class WorkingFolderData
{
    private String serverItem;
    private String localItem;
    private WorkingFolderType type;

    public WorkingFolderData(WorkingFolder workingFolder)
    {
        Check.notNull(workingFolder, "workingFolder");
        if (workingFolder.getDepth() == RecursionType.ONE_LEVEL) {
            this.serverItem = ServerPath.combine(workingFolder.getServerItem(), "*");
        } else {
            this.serverItem = workingFolder.getServerItem();
        }
        this.localItem = workingFolder.getLocalItem();
        this.type = workingFolder.getType();
        if (this.type == null) {
            this.type = WorkingFolderType.MAP;
        }
    }

    public WorkingFolderData(String serverItem, String localItem, WorkingFolderType type)
    {
        this.serverItem = serverItem;
        this.localItem = localItem;
        this.type = type;
    }

    public String toString()
    {
        return (this.type == null ? "[no type set]" : this.type.toString()) + " " + (this.serverItem == null ? "[no server item set]" : new StringBuilder().append("[").append(this.serverItem).append("]").toString()) + " => " + (this.localItem == null ? "[no local item set]" : new StringBuilder().append("[").append(this.localItem).append("]").toString());
    }

    public WorkingFolder createWorkingFolder()
    {
        if ((this.serverItem == null) || (this.serverItem.trim().length() == 0)) {
            throw new IllegalStateException("serverItem is null or empty");
        }
        if (this.type == null) {
            throw new IllegalStateException("type is null");
        }
        if ((WorkingFolderType.CLOAK != this.type) && ((this.localItem == null) || (this.localItem.trim().length() == 0))) {
            throw new IllegalStateException("localItem is null or empty and working folder is not a cloak");
        }
        RecursionType recursionType;
        String workingFolderServerItem;
        if ((WorkingFolderType.CLOAK != this.type) && ("*".equals(ServerPath.getFileName(this.serverItem))))
        {
            workingFolderServerItem = ServerPath.getParent(this.serverItem);
            recursionType = RecursionType.ONE_LEVEL;
        }
        else
        {
            workingFolderServerItem = this.serverItem;
            recursionType = RecursionType.FULL;
        }
        return new WorkingFolder(workingFolderServerItem, LocalPath.canonicalize(this.localItem), this.type, recursionType);
    }

    public String getLocalItem()
    {
        return this.localItem;
    }

    public void setLocalItem(String localItem)
    {
        this.localItem = localItem;
    }

    public String getServerItem()
    {
        return this.serverItem;
    }

    public void setServerItem(String serverItem)
    {
        this.serverItem = serverItem;
    }

    public WorkingFolderType getType()
    {
        return this.type;
    }

    public void setType(WorkingFolderType type)
    {
        this.type = type;
    }

    public boolean isCloak()
    {
        return WorkingFolderType.CLOAK == this.type;
    }
}
